package com.mr.phone;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;

public class MrPhone {

    public static class MapWriter extends Mapper<LongWritable, Text, Text, FlowBean> {
        private static final String SPLITTER = "\t";

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            // 切分一行数据
            String[] fields = line.split(SPLITTER);
            // 数组长度
            int len = fields.length;
            // 上行流量,倒数第三个字段
            long upFlow = Long.parseLong(fields[len - 3]);
            // 下行流量,倒数第二个字段
            long downFlow =  Long.parseLong(fields[len - 2]);
            // 手机号
            String phoneNumber = fields[1];

            FlowBean one = new FlowBean(upFlow, downFlow);
            context.write(new Text(phoneNumber), one);
        }
    }

    public static class ReduceWriter extends Reducer<Text, FlowBean, Text, FlowBean> {
        @Override
        public void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
            //1.定义两个计数器，计算每个用户的上传流量、下载流量
            long sumupflow = 0;
            long sumdownflow = 0;

            //2.累加的号的流量和
            for (FlowBean f: values) {
                sumupflow += f.getUpFlow();
                sumdownflow += f.getDownFlow();
            }

            //3.输出
            context.write(key, new FlowBean(sumupflow, sumdownflow));
        }
    }

    public static void main(String[] args) throws Exception {
        if (args == null || args.length < 2) {
            // 输入位置, 输出位置
            System.out.println("缺少输入路径/输出路径参数");
            System.exit(0);
        }

        // 1.获取job信息
        String jobName = "MrPhoneGood";
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, jobName);

        // 2.加载jar包
        job.setJarByClass(MrPhone.class);
        // 3.关联map和reduce
        job.setMapperClass(MrPhone.MapWriter.class);
        job.setCombinerClass(MrPhone.ReduceWriter.class);
        job.setReducerClass(MrPhone.ReduceWriter.class);

        // 4.设置最终输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowBean.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);
        job.setNumReduceTasks(2);

        // 5. 输入文件路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        // 6. 输出文件路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        // 7. 提交job任务
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
